package com.zhiyou.app.common.constant;

import cn.hutool.crypto.SecureUtil;
import cn.hutool.crypto.symmetric.SymmetricAlgorithm;
import cn.hutool.crypto.symmetric.SymmetricCrypto;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

@Component
public class EncryptedTool {
    @Value("${encrypted.salt:ElkyDWNP9DMcsyoH1gDhYW10LrEFzsho}")
    private String salt;

    private SymmetricCrypto aes;

    @PostConstruct
    public void init() {
        byte[] key = SecureUtil.generateKey(SymmetricAlgorithm.AES.getValue(), (salt).getBytes()).getEncoded();
        // 创建对称加密器对象
        aes = new SymmetricCrypto(SymmetricAlgorithm.AES, key);
    }

    public String encode(String originContent) {
        return aes.encryptHex(originContent);
    }

    public String decode(String encryptedContent) {
        return aes.decryptStr(encryptedContent);
    }
}
